package com.yellow.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yellow.constant.MessageConstant;
import com.yellow.dao.MemberDao;
import com.yellow.dao.OrderDao;
import com.yellow.dao.OrderSettingDao;
import com.yellow.dao.SetMealDao;
import com.yellow.enity.Result;
import com.yellow.pojo.Member;
import com.yellow.pojo.Order;
import com.yellow.pojo.OrderSetting;
import com.yellow.pojo.Setmeal;
import com.yellow.service.OrderService;
import com.yellow.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetMealDao setMealDao;

    @Override
    public Result order(Map map) throws Exception {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        //查询ordersetting表
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //以开始预约设置
//        2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            //预约人数已满
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约
        //则无法完成再次预约

        //用户id
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findMemberByTelNum(telephone);
        if (member == null) {
            //用户还不是会员
            //添加会员
            String name = (String) map.get("name");
            String idCard = (String) map.get("idCard");
            String sex = (String) map.get("sex");
            member = new Member(name, sex, idCard, telephone, new Date());
            //public Member(String name, String sex, String idCard, String phoneNumber, Date regTime) {
            //返回自增主键
            memberDao.add(member);
        }
        Integer memberId = member.getId();
        //预约时间
        String order_Date = (String) map.get("orderDate");
        //预约套餐
        Integer setmealId = Integer.parseInt((String) map.get("setmealId"));

        Order _order = new Order(memberId, DateUtils.parseString2Date(order_Date), setmealId);
        List<Order> orders = orderDao.findOrderBycondition(_order);
        if (orders.size() == 0) {
            //不是重复预约
            //返回自增主键，订单编号
            //public Order(Integer memberId, Date orderDate, String orderType, String orderStatus, Integer setmealId) {
            Order order = new Order(memberId, DateUtils.parseString2Date(orderDate), (String) map.get("orderType"), Order.ORDERSTATUS_NO, setmealId);
            orderDao.addOrder(order);
            //更新当日预约人数
            OrderSetting orderSetting_new = new OrderSetting(orderSetting.getId(), orderSetting.getReservations() + 1);
            orderSettingDao.updateOrderSetting(orderSetting_new);
            return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());

        }
        return new Result(false, MessageConstant.HAS_ORDERED);
    }

    @Override
    public Map findById(int id) {
//        Order order = orderDao.findOrderByid(id);
//        //获取会员id
//        Integer memberId = order.getMemberId();
//        Member member = memberDao.findMemberById(memberId);
//        //获取套餐id
//        Integer setmealId = order.getSetmealId();
//        Setmeal setmeal = setMealDao.findSetMealByid(setmealId);
//        Map<String, Object> map = new HashMap<>();
//        map.put("member", member.getName());
//        map.put("setmeal", setmeal.getName());
//        map.put("orderDate", order.getOrderDate());
//        map.put("orderType", order.getOrderType());

        Map<String, Object> map = orderDao.findOrderDetailByid(id);
//        System.out.println("------>map = " + map);

        return map;
    }
}
